package com.example.exam.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.exam.mapper.UserMapper;
import com.example.exam.pojo.User;
import com.example.exam.service.UserService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zhozixiong
 * @date 2023/03/07
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public int insert(User user) {
        return baseMapper.insert(user);
    }

    @Override
    public User searchAllBySid(String sid) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("sid", sid);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public List<User> searchAchievementList() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.ne("achievement", -1);
        wrapper.orderByDesc("achievement");
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<User> searchAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public List<User> searchGrade() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("language");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void login(User user, Date loginTime) {
        Date date = new Date();
        if(loginTime == null) {
            user.setLoginTime(date);
        }
        user.setRecentlyTime(date);
        baseMapper.login(user);
    }

    @Override
    public void updateLanguageBySid(String sid) {
        baseMapper.updateLanguageBySid(sid);
    }

    @Override
    public void updateAchievement(User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("sid", user.getSid());
        baseMapper.update(user, updateWrapper);
    }

    @Override
    public void delectAchievement(String sid) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("sid", sid);
        baseMapper.delete(wrapper);
    }
}
